//logs.js
const app = getApp()

Page({
    data: {
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        isLogin:false,
        isClick:0,
        status:0,//1，审核通过了   2审核未通过 3，审核z中  0，默认  0显示上传的界面 1直接跳过 2在显示未通过 然后点击进入上传界面3，审核中
        userType:0,
    },
    onLoad: function () {
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        this.getUserInfo();
        this.getUserData();
        //0 普通用户 ，1 跑腿的
        var userType = wx.getStorageSync('userType')
        this.setData({
            userType:userType||0
        });
    },
    getUserInfo: function () {
        var userinfo = app.util.getUserInfo();
        if (userinfo && userinfo.nickName) {
            this.setData({
                userInfo: userinfo,
                hasUserInfo: true,
                hasUserInfo:true,
                isLogin:true
            })
        }

    },
    onShow:function(){
        this.switchIdentityInfo();
    },
    switchIdentity:function () {
        var that = this;

        if(that.data.isClick) return;
        that.data.isClick = 1;
        var userType = that.data.userType;

        wx.showModal({
            title:'是否切换当前身份为' + (userType?'普通用户':'跑腿者'),
            showCancle:true,
            success:function (res) {
                if (res.confirm) {
                    wx.showToast({
                        icon:'loading',
                        title:'加载中'
                    })
                    var data = !userType
                    that.setData({
                        userType:data
                    })
                    wx.setStorageSync('userType',data);
                    wx.hideToast();
                }
                setTimeout(function () {
                    that.data.isClick = 0;
                },1000)

            }
        })
    },
    switchIdentityInfo:function(){
        var that = this;
        if(that.data.isClick) return;
        that.data.isClick = 1;

        app.util.request({
            url: app.globalRequestUrl + '/change_auth',
            method:'GET',
            success: function (res) {
                if (res && res.msg == 'success') {
                    that.setData({
                        status:res.status||0
                    });
                }
            },
            complete: function () {
                wx.hideToast();
                that.data.isClick = 0;
            }
        });
    },
    onShareAppMessage: function (res) {
        return {
            title: '邀请好友，一起来跑腿',
            path: '/pages/index/index',
        }
    },
    login:function (e) {
        var that = this;
        if(that.data.isClick) return;
        that.data.isClick = 1;
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        app.util.setUserInfo(e);
        wx.login({//调用登录获取code换取openId和sessionkey
            success: function (res) {
                var code = res.code;
                if (code){
                    var data = {
                        code:code,
                    }
                    app.util.request({
                        url: app.globalRequestUrl + '/return_id',
                        data: data,
                        success: function (res) {
                            if (res && res.msg == 'success') {
                                var info = app.util.getUserInfo();
                                if(info){
                                    that.loginUid({
                                        nick_name:info.nickName,
                                        avatarurl:info.avatarUrl,
                                        openid:res.data
                                    });
                                }
                            }
                        },
                        complete: function () {
                            // console.log(app.util.getUserInfo());
                        }
                    });

                }else {
                    console.log('获取用户登录态失败！' + res.msg)
                }
            }
        });
    },
    loginUid:function (data) {
        var that = this;
        app.util.request({
            url: app.globalRequestUrl + '/getmation',
            data: data,
            success: function (res) {
                if (res && res.data) {
                    var info = app.util.getUserInfo();
                    that.setData({
                        isLogin:true,
                        userInfo: info
                    });
                    app.util.setCookies(res.data)
                    that.switchIdentityInfo();
                }
            },
            complete: function () {
                wx.hideToast();
            }
        });
    },
    getUserData:function (data) {
        var that = this;
        app.util.request({
            url: app.globalRequestUrl + '/manyuser',
            method:'GET',
            data: data,
            success: function (res) {
                if (res && res.data) {
                    // console.log(data);
                }
            },
            complete: function () {
                // console.log(app.util.getUserInfo());
            }
        });
    },
    bindAuth:function () {
        var status = this.data.status;
        wx.navigateTo({
            url: "/pages/auth/auth?status=" + status
        })
    },
    logOut:function () {
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        wx.clearStorageSync();
        this.setData({
            userInfo: {},
            hasUserInfo: false,
            isLogin:false,
            isClick:0,
            status:0,
            userType:0
        })
        wx.hideToast();

    }
})
